package com.spdb.spdbfl.restful.project;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spdb.spdbfl.config.BaseConfig;
import com.spdb.spdbfl.utils.ConfigProperty;
import com.spdb.spdbfl.utils.GetBaseDataUtil;
import com.spdb.spdbfl.utils.HttpRequest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


@Test(description = "审批流程")
public class ApproveIT {

    public static final String todoUrl = "/process/task/todo";
    static String domainUrl = BaseConfig.domainUrl;;
    static String token = "";
    static String url = "";//我的待办url
    static String assistManager = "ff8080816bd64342016bd9b91f010000";
    static String custId = "9a6264a6a46d4de9a004ca7261b18d19";
    static String custName = "徐工集团工程机械股份有限公司";
    static String director = "8af4e52656c4af8a0156c58df49c0022";
    static String manager = "8af4e52656c4af8a0156c59ba4f30047";

    @BeforeClass
    public void setUp() throws Exception {
        token = ConfigProperty.getProperty("liumToken");
        url = domainUrl + todoUrl;
    }

    @Test(description = "我的待办")
    public static void todo(){
        String param = todoParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);

        JSONObject jsonResult = JSONObject.parseObject(result);
        JSONObject data = (JSONObject) jsonResult.get("data");
        JSONArray dataList = (JSONArray) data.get("dataList");
        JSONObject jsonObject = (JSONObject) dataList.get(0);
        String processInstanceId = (String)jsonObject.get("processInstanceId");
        String taskId = (String)jsonObject.get("taskId");
//        System.out.println("processInstanceId:"+processInstanceId);
//        System.out.println("taskId:"+taskId);
        ConfigProperty.updateProperty("instanceId", processInstanceId);
        ConfigProperty.updateProperty("taskId", taskId);
    }

    @Test(description = "协同审批")
    public static void approval(){
        String param = approvalParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);
    }

    /**
     * 构建approval入参
     * @return
     */
    private static String approvalParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("account", null);
        jsonObject.put("accountCheck", "0");
        jsonObject.put("allJudges", null);
        jsonObject.put("apiCaller", "01");
        jsonObject.put("appendSql", null);

        JSONObject applicationJson = new JSONObject();
        applicationJson.put("applicationId", null);
        applicationJson.put("applyDate", 1665210765000L);
        applicationJson.put("applyState", "2");
        applicationJson.put("businessId", ConfigProperty.getProperty("projectApprovalId"));
        JSONObject contentJson = new JSONObject();
        contentJson.put("accountCheck", "0");
        contentJson.put("applyId", "20221008143242069");
        contentJson.put("assistManager", assistManager);
        contentJson.put("assistManagerRatio", "0");
        contentJson.put("accountCheck", 7);
        contentJson.put("bizDept", "10");
        contentJson.put("bizSource", "02");
        contentJson.put("bizType", "4-10");
        contentJson.put("custId", custId);
        contentJson.put("custName", custName);
        contentJson.put("deptName", "");
        contentJson.put("deputyInvolved", "0");
        contentJson.put("director", director);
        contentJson.put("id", ConfigProperty.getProperty("applicationId"));
        contentJson.put("isUrgent", "false");
        contentJson.put("leaseType", "3-01-02");
        JSONArray leaseTypeArray = new JSONArray();
        leaseTypeArray.add("3-01-02");
        contentJson.put("leaseTypeList", leaseTypeArray);
        contentJson.put("mainManagerRatio", 7);
        contentJson.put("manager", manager);
        contentJson.put("projectId", ConfigProperty.getProperty("projectId"));
        contentJson.put("projectName", ConfigProperty.getProperty("projectName"));
        contentJson.put("recallFlag", "0");
        contentJson.put("riskRating", "AA");
        applicationJson.put("content", contentJson);
        applicationJson.put("createTime", 1665210763000L);
        applicationJson.put("creator", manager);
        applicationJson.put("custId", custId);
        applicationJson.put("deptId", "10");
        applicationJson.put("id", ConfigProperty.getProperty("applicationId"));
        applicationJson.put("isDelete", "N");
        applicationJson.put("liborRateId", null);
        applicationJson.put("processDefinitionKey", "ProjectApproval");
        applicationJson.put("processDefinitionName", "项目审批");
        applicationJson.put("processInstanceId", ConfigProperty.getProperty("instanceId"));
        applicationJson.put("projectApprovalId", null);
        applicationJson.put("projectId", ConfigProperty.getProperty("projectId"));
        applicationJson.put("projectRecheckId", null);
        applicationJson.put("requisitionFinanceId", null);
        applicationJson.put("requisitionLoanId", null);
        applicationJson.put("requisitionProjectId", null);
        applicationJson.put("routerUrl", "workflow_project_approval");
        applicationJson.put("staffId", ConfigProperty.getProperty("staffId"));
        applicationJson.put("status", null);
        applicationJson.put("taskId", manager);
        applicationJson.put("updateTime", 1665210765000L);
        applicationJson.put("updater", manager);
        jsonObject.put("application", applicationJson);

        jsonObject.put("applyId", null);
        jsonObject.put("applyState", null);
        jsonObject.put("approvalComment", "agree");
        jsonObject.put("approveOpinionSummaryExt", null);
        jsonObject.put("assistManager", GetBaseDataUtil.getUserId("liuhy"));
        jsonObject.put("assistManagerRatio", 7);
        jsonObject.put("attachmentBase", null);
        jsonObject.put("attachmentBaseList", null);
        jsonObject.put("backDefinitionKey", null);
        jsonObject.put("bizDept", "10");
        jsonObject.put("bizDeptName", null);
        jsonObject.put("bizSource", "02");
        jsonObject.put("checkOpinion", null);
        jsonObject.put("content", null);
        jsonObject.put("creationBy", GetBaseDataUtil.getUserId("liuc"));
        jsonObject.put("creationDate", 1665210763000L);
        jsonObject.put("currentNodeName", "assist_manager");
        jsonObject.put("currentRole", null);
        jsonObject.put("currentUser", null);
        jsonObject.put("currnetPersionName", null);

        JSONObject custJson = new JSONObject();
        custJson.put("antiMoneyRegiId", null);
        custJson.put("approvalTime", null);
        custJson.put("assistManager", null);
        custJson.put("baseAccSts", null);
        custJson.put("birthDate", null);
        custJson.put("birthDateStr", "");
        custJson.put("bizCode", null);
        custJson.put("bizTime", null);
        custJson.put("bizTimeStr", "");
        custJson.put("bussyUnit", null);
        custJson.put("checkSts", null);
        custJson.put("contactInfoEntity", null);
        custJson.put("country", "CHN");
        custJson.put("countryName", null);
        custJson.put("createperson", manager);
        custJson.put("createtime", 1658123117000L);
        custJson.put("creditAmount", null);
        custJson.put("creditCurrency", null);
        custJson.put("custBankEntity", null);
        custJson.put("custCode", "C134793499");
        custJson.put("custFlag", "1");
        custJson.put("custForeignName", "");
        custJson.put("custIdentity", null);
        custJson.put("custIdentityName", null);
        custJson.put("custName", "徐工集团工程机械股份有限公司");
        custJson.put("custType", "1-10");
        custJson.put("custTypeName", null);
        custJson.put("deleteFlag", "N");
        custJson.put("district", "320000-320300-320305");
        custJson.put("districtCity", null);
        custJson.put("districtCounty", null);
        custJson.put("districtName", null);
        custJson.put("districtProvince", null);
        custJson.put("educationalAttainments", null);
        custJson.put("enterprieseNature", "10-16");
        custJson.put("enterprieseNatureName", null);
        custJson.put("establishTime", null);
        custJson.put("establishTimeStr", "");
        custJson.put("familyAvgIncome", null);
        custJson.put("familyMonthlyIncome", null);
        custJson.put("flags", null);
        custJson.put("idNbr", null);
        custJson.put("idType", null);
        custJson.put("idTypeName", null);
        custJson.put("industryCatNew", "C-35-351-3514");
        custJson.put("industryCategory", "C-35-351-3514");
        custJson.put("industryCategoryName", null);
        custJson.put("industryTypeName", null);
        custJson.put("isCheckPass", "1");
        custJson.put("isCredit", "1");
        custJson.put("isFinance", "0");
        custJson.put("isGroup", "1");
        custJson.put("isGroupName", null);
        custJson.put("isLeaseCompanyRelaParty", "0");
        custJson.put("isLeaseCompanyShareRelaParty", "0");
        custJson.put("isListed", "1");
        custJson.put("isServiceTrace", "0");
        custJson.put("isServiceTraceType", null);
        custJson.put("isSpv", null);
        custJson.put("isSpvName", null);
        custJson.put("isStateRestrictedIndustry", "0");
        custJson.put("loanCode", "3203010002574479");
        custJson.put("mainEconomicSource", null);
        custJson.put("mainManager", manager);
        custJson.put("mainManagerName", null);
        custJson.put("maritalStatus", null);
        custJson.put("modifyperson", manager);
        custJson.put("modifytime", 1665210763000L);
        custJson.put("orgCode", "67133292-X");
        custJson.put("orgCrdNo", null);
        custJson.put("orgFlag", null);
        custJson.put("ownerGroup", "9a6264a6a46d4de9a004ca7261b18d19");
        custJson.put("ownerGroupName", null);
        custJson.put("projectId", null);
        custJson.put("projectName", null);
        custJson.put("projectSts", null);
        custJson.put("provinceCityCounty", null);
        custJson.put("referenceid", null);
        custJson.put("region", "01");
        custJson.put("regionName", null);
        custJson.put("registerCapital", 7833668400L);
        custJson.put("registerCurrency", "CNY");
        custJson.put("registeredResidence", null);
        custJson.put("registeredResidenceName", null);
        custJson.put("relationPersonType", null);
        custJson.put("remarks", null);
        custJson.put("riskLevel", null);
        custJson.put("riskRating", "AA");
        custJson.put("riskScore", null);
        custJson.put("riskScoreStr", null);
        custJson.put("scale", "2");
        custJson.put("sex", null);
        custJson.put("sharePorperty", "01");
        custJson.put("sharePorpertyName", null);
        custJson.put("socialCreditCode", "913203001347934993");
        custJson.put("sts", "1");
        custJson.put("stsTime", null);
        custJson.put("tableid", custId);
        custJson.put("title", null);
        jsonObject.put("cust", custJson);

        jsonObject.put("custId", custId);
        jsonObject.put("custName", custName);
        jsonObject.put("deptCodes", null);
        jsonObject.put("deptName", "");
        jsonObject.put("deputyInvolved", "0");
        jsonObject.put("director", director);
        jsonObject.put("docList", null);
        jsonObject.put("earinGrates", "5");
        jsonObject.put("id", ConfigProperty.getProperty("applicationId"));
        jsonObject.put("instanceId", ConfigProperty.getProperty("instanceId"));
        jsonObject.put("instanceIdList", null);
        jsonObject.put("irr", 5);
        jsonObject.put("isChairman", null);
        jsonObject.put("isDelete", "N");
        jsonObject.put("isUrgent", "0");
        jsonObject.put("judgesConveningTime", null);
        jsonObject.put("judgesNumber", null);
        jsonObject.put("leaseProjectExts", null);
        jsonObject.put("leaseTime", "12");
        jsonObject.put("leaseType", "3-01-02");
        jsonObject.put("leaseTypeList", null);
        jsonObject.put("mainManagerRatio", 7);
        jsonObject.put("manager", manager);
        jsonObject.put("menuId", null);
        jsonObject.put("modificationBy", manager);
        jsonObject.put("modificationDate", 1665210764000L);
        jsonObject.put("node", "assist_manager");
        jsonObject.put("path", "yes");
        jsonObject.put("processDefinitionId", null);
        jsonObject.put("projectApprovalId", ConfigProperty.getProperty("projectApprovalId"));
        jsonObject.put("projectId",  ConfigProperty.getProperty("projectId"));

        JSONObject projectInfoJson = new JSONObject();
        projectInfoJson.put("addflag", null);
        projectInfoJson.put("approvalRecheck", null);
        projectInfoJson.put("approveAmount", null);
        projectInfoJson.put("approveTime", null);
        projectInfoJson.put("assistManager", assistManager);
        projectInfoJson.put("assistManagerRatio", 7);
        projectInfoJson.put("bizDept", "10");
        projectInfoJson.put("bizDeptName", null);
        projectInfoJson.put("bizSource", "02");
        projectInfoJson.put("bizSourceName", null);
        projectInfoJson.put("bizType", "4-10");
        projectInfoJson.put("branch", null);
        projectInfoJson.put("branchName", null);
        projectInfoJson.put("businessType", "1-04");
        projectInfoJson.put("businessTypeName", null);
        projectInfoJson.put("bussyUnit", null);
        projectInfoJson.put("chanceOrProject", "1");
        projectInfoJson.put("changestuffflag", null);
        projectInfoJson.put("createperson", manager);
        projectInfoJson.put("createtime", 1665210762000L);
        projectInfoJson.put("credit", null);
        projectInfoJson.put("creditUse", null);
        projectInfoJson.put("crossMarket", null);
        projectInfoJson.put("currency", "CNY");
        projectInfoJson.put("currencyName", null);
        projectInfoJson.put("cust", null);
        projectInfoJson.put("deleteFlag", "N");
        projectInfoJson.put("deleteflag", null);
        projectInfoJson.put("depositAmount", null);
        projectInfoJson.put("deptName", "");
        projectInfoJson.put("detpStaffid", null);
        projectInfoJson.put("earinGrate", "5");
        projectInfoJson.put("editflag", null);
        projectInfoJson.put("f", false);
        projectInfoJson.put("finanAmount", null);
        projectInfoJson.put("fiveClass", "A-1");
        projectInfoJson.put("flag", null);
        projectInfoJson.put("flags", null);
        projectInfoJson.put("fromRemark", "这是项目简要说明");
        projectInfoJson.put("fromRemarkName", "这是项目简要说明");
        projectInfoJson.put("industryCategory", "C-02");
        projectInfoJson.put("isAbs", null);
        projectInfoJson.put("isBuyout", null);
        projectInfoJson.put("isEffect", null);
        projectInfoJson.put("isUrgent", false);
        projectInfoJson.put("leaseAmount", 1000000000);
        projectInfoJson.put("leaseItem", null);
        projectInfoJson.put("leaseModel", "01");
        projectInfoJson.put("leaseTerm", "12");
        projectInfoJson.put("leaseType", "3-01-02");
        projectInfoJson.put("leaseTypeName", null);
        projectInfoJson.put("mainManager", manager);
        projectInfoJson.put("mainManagerName", null);
        projectInfoJson.put("mainManagerRatio", null);
        projectInfoJson.put("meetingTime", null);
        projectInfoJson.put("modifyperson", manager);
        projectInfoJson.put("modifytime", 1665210764000L);
        projectInfoJson.put("passTime", null);
        projectInfoJson.put("proProjectId", null);
        projectInfoJson.put("proejctType", null);
        projectInfoJson.put("proejctTypeName", null);
        projectInfoJson.put("projectCode", "PRJ-202210080004");
        projectInfoJson.put("projectDesc", null);
        projectInfoJson.put("projectName", "project20221008143207");
        projectInfoJson.put("purpose", null);
        projectInfoJson.put("queryleaseType1", null);
        projectInfoJson.put("queryleaseType2", null);
        projectInfoJson.put("referee", null);
        projectInfoJson.put("remarks", null);
        projectInfoJson.put("replyAmount", null);
        projectInfoJson.put("replyTime", null);
        projectInfoJson.put("reportAmount", null);
        projectInfoJson.put("researchFlag", null);
        projectInfoJson.put("schedule", null);
        projectInfoJson.put("sign", "0");
        projectInfoJson.put("staffEntities", null);
        projectInfoJson.put("sts", "07");
        projectInfoJson.put("stsName", null);
        projectInfoJson.put("stsTime", 1665210765000L);
        projectInfoJson.put("tableid", ConfigProperty.getProperty("projectId"));
        projectInfoJson.put("taxType", null);

        jsonObject.put("projectInfo", projectInfoJson);
        jsonObject.put("projectName", ConfigProperty.getProperty("projectName"));
        jsonObject.put("projectStatus", "02");
        jsonObject.put("purchasePrincipal", 1000000000);
        jsonObject.put("recallFlag", "0");
        jsonObject.put("rejectId", null);
        jsonObject.put("requisitionResponseId", null);
        jsonObject.put("returnFlag", "0");
        jsonObject.put("riskRating", "AA");
        jsonObject.put("showpurchasePrincipal", "100,000.00");
        jsonObject.put("startStaffName", null);
        jsonObject.put("startTime", null);
        jsonObject.put("sts", null);
        jsonObject.put("targetId", null);

        JSONObject taskAssigneeListJson = new JSONObject();
        taskAssigneeListJson.put("assignee", director);
        taskAssigneeListJson.put("assigneeVarName", "workflow_project_approval_dept");
        taskAssigneeListJson.put("taskDefId", "dept_head_approval");
        JSONArray taskAssigneeArray = new JSONArray();
        taskAssigneeArray.add(taskAssigneeListJson);

        jsonObject.put("taskAssigneeList", taskAssigneeArray);
        jsonObject.put("taskId", ConfigProperty.getProperty("taskId"));
        jsonObject.put("titleName", null);
        return jsonObject.toJSONString();
    }

    /**
     * 构建todo入参
     * @return
     */
    private static String todoParam() {
        String userId01 = GetBaseDataUtil.getUserId("liuhy");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageNum", 1);
        jsonObject.put("pageSize", 1);
        jsonObject.put("userId", userId01);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("title", "");
        jsonObject.put("processTaskSearchDto", jsonObject1);
        return jsonObject.toJSONString();
    }


    @Test
    public static void approve(){

    }

}
